package spring6_learn.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(2)//控制多个切面的顺序，越小越先执行
public class AopService {
    /*执行顺序
      前环绕
      before
      保存user数据
      after
      finally*/
    @Pointcut("execution(* spring6_learn.service..*(..))")//同意配置切点
    public void 通用切点(){

    }

    //前置通知
    @Before("通用切点()")
    public void before(JoinPoint joinPoint)//JoinPoint可以获取到方法的签名等信息,spring自动传入
    {
        System.out.println("before");
        Signature signature = joinPoint.getSignature();//获取方法签名例如public void before
        signature.getModifiers();
        System.out.println(signature.getModifiers());//获取方法修饰符
        System.out.println(signature.getName());//获取方法名
        System.out.println(signature.getDeclaringTypeName());//获取方法所在的类名
        System.out.println(signature.getDeclaringType());//获取方法所在的类

    }

    //后置通知(运行后)
    @AfterReturning("通用切点()")
    public void after()
    {
        System.out.println("after");
    }

    //环绕通知
    @Around("execution(* spring6_learn.service..*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("前环绕");
        joinPoint.proceed();
        System.out.println("后环绕");
    }

    //异常通知
    @AfterThrowing("execution(* spring6_learn.service..*(..))")
    public void exception()
    {
        System.out.println("exception");
    }

    //最后通知
    @After("execution(* spring6_learn.service..*(..))")
    public void final_ly()
    {
        System.out.println("finally");
    }
}
